package com.esreport.controller;

import com.alibaba.fastjson.JSONObject;
import com.esreport.Utils.ResultJson;
import com.esreport.dto.Site;
import com.esreport.entity.Statistics;
import com.esreport.service.SiteService;
import com.esreport.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @ClassName StatisticsController
 * @Description
 * @Author fanxingwei
 * @Date 2020/8/26 15:19
 * @Version 1.0
 */
@Controller
@RequestMapping("/statistics")
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    @Autowired
    private SiteService siteService;

    @Resource
    private HttpServletRequest request;

    @Resource
    private HttpServletResponse response;


    @RequestMapping("/addData")
    public String addData(ModelMap modelMap){
        return "statistics_add";
    }

    @RequestMapping("/save")
    @ResponseBody
    public String save(Integer IOSDownloadsNum, Integer AndroidDownloadsNum,Integer installNum){
        Integer siteId = (Integer) request.getSession().getAttribute("siteId");
        Statistics statistics = new Statistics();
        statistics.setAndroidDownloadsNum(AndroidDownloadsNum);
        statistics.setIOSDownloadsNum(IOSDownloadsNum);
        statistics.setInstallNum(installNum);
        statistics.setDate(new Date());
        statistics.setSiteId(siteId);
        statisticsService.save(statistics);
        return ResultJson.successMeg("保存成功");
    }

    @RequestMapping("/index")
    public String list(ModelMap modelMap){
        Integer siteId = (Integer) request.getSession().getAttribute("siteId");
        List<Site> siteList = siteService.list();
        if(siteId == null){
            siteId = siteList.get(0).getSiteId();
            request.getSession().setAttribute("siteId",siteId);
        }
        modelMap.put("siteList",siteList);
        modelMap.put("siteId",siteId);
        return "statistics_list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(){
        Integer siteId = (Integer) request.getSession().getAttribute("siteId");
        List<Statistics> set = statisticsService.getList(siteId);
        JSONObject json = new JSONObject();
        json.put("status", 200);
        json.put("rows", set);
        json.put("total", set.size());
        json.put("msg", "请求成功");
        return json.toJSONString();
    }

    @RequestMapping("/edit")
    public String edit(Integer id,ModelMap modelMap){
        Statistics entity = statisticsService.getOne(id);
        modelMap.put("statistics",entity);
        return "statistics_edit";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(Integer id,Integer IOSDownloadsNum, Integer AndroidDownloadsNum,Integer installNum){

        Statistics entity = statisticsService.getOne(id);
        entity.setIOSDownloadsNum(IOSDownloadsNum);
        entity.setAndroidDownloadsNum(AndroidDownloadsNum);
        entity.setInstallNum(installNum);

        statisticsService.update(entity);
        return ResultJson.successMeg("修改完成");
    }

    @RequestMapping("/del")
    @ResponseBody
    public String del(Integer id){
        statisticsService.del(id);
        return ResultJson.successMeg("删除完成");
    }

    /**
     * 重新向session中放入siteID  以便后面的功能使用
     * @param siteId
     * @return
     */
    @GetMapping("setSiteId/{siteId}")
    @ResponseBody
    public String setSite(@PathVariable Integer siteId){
        HttpSession session = request.getSession();
        session.setAttribute("siteId",siteId);
        return ResultJson.successMeg("");
    }





}
